
package gui;

import java.awt.Color;
import java.util.ArrayList;

import processing.core.PApplet;

public class ParallelCoordinatesPlot {

	private String[] headers;
	private double[][] minMax;
	private ArrayList<ParallelCoordinatesPlotLine> lines;
	private double[][] data;
	private PApplet mainApplet;
	private int posX;
	private int posY;
	private int width;
	private int height;
	private int horizontalGap;
	private int verticalGap;
	private int axisColor;
	private int lineColor;
	private int selectedColor;
	
	private int lineLength;
	
	private int[][] axis;
	
	/**
	 * Creates a new Parallel coordinates plot
	 * @param mainApplet the applet where its going to be drawn
	 * @param headers headers of each coordinate
	 * @param minMax
	 * @param posX
	 * @param posY
	 * @param width
	 * @param nHeight
	 * @param horizontalGap
	 * @param verticalGap
	 * @param axisColor
	 */
	public ParallelCoordinatesPlot(PApplet mainApplet, String[] headers, double[][] minMax, int posX, int posY, int width, int nHeight, int horizontalGap, int verticalGap, int axisColor)
	{
		this.mainApplet = mainApplet;
		this.headers = headers;
		this.minMax = minMax;
		this.posX = posX;
		this.posY = posY;
		this.width = width;
		this.height = nHeight;
		this.horizontalGap = horizontalGap;
		this.verticalGap = verticalGap;
		this.axisColor = axisColor;
		
		this.lines = new ArrayList<ParallelCoordinatesPlotLine>();
		this.axis = new int[headers.length][4];
		
		lineLength = height - (2*verticalGap);
		
		axis[0][0] = posX + horizontalGap;
		axis[0][1] = posY + verticalGap;
		axis[0][2] = posX + horizontalGap;
		axis[0][3] = posY + height - verticalGap;
		
		int axisGap = (int)((double)(width - (2*horizontalGap)) / (double)(headers.length - 1));
		for(int i = 2; i < headers.length; i++)
		{
			axis[i - 1][0] = posX + horizontalGap + ((i-1)*axisGap);
			axis[i - 1][1] = posY + verticalGap;
			axis[i - 1][2] = posX + horizontalGap + ((i-1)*axisGap);
			axis[i - 1][3] = posY + height - verticalGap;
		}
		
		axis[headers.length - 1][0] = posX + (width - horizontalGap);
		axis[headers.length - 1][1] = posY + verticalGap;
		axis[headers.length - 1][2] = posX + (width - horizontalGap);
		axis[headers.length - 1][3] = posY + height - verticalGap;
		
		Color lColor = Color.WHITE;
		this.lineColor = lColor.getRGB();
		Color sColor = Color.YELLOW;
		this.selectedColor = sColor.getRGB();
	}
	
	
	public void setData(double[][] data)
	{
		this.data = data;
		lines.clear();
		
		for(int i = 0; i < data.length; i++)
		{
			double[] lineData = data[i];
			int[][] points = new int[lineData.length][2];
			for(int j = 0; j < lineData.length; j++)
			{
				points[j][0] = axis[j][0];
				points[j][1] = posY + height - verticalGap - (int)PApplet.map((float)lineData[j], (float)minMax[j][0], (float)minMax[j][1], 0, (float)lineLength);
			}
			lines.add(new ParallelCoordinatesPlotLine(mainApplet, points, lineColor, selectedColor));
		}
	}
	
	public void drawPlot()
	{
		//Draw Axis
		for(int i = 0; i < axis.length; i++)
		{
			mainApplet.stroke(axisColor);
			mainApplet.line(axis[i][0], axis[i][1], axis[i][2], axis[i][3]);
		}
		
		//Draw Data
		if(!lines.isEmpty())
		{
			for(ParallelCoordinatesPlotLine line : lines)
			{
				line.drawLine();
			}
		}
	}
}
